﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using DBProvider.Providers;
using DBProvider;

namespace HealthCommunity.Controls
{
	public partial class UCCourseConfirm : System.Web.UI.UserControl
	{
		private ICourseProvider _CourseProvider;

		public List<int> CourseIDs
		{
			get
			{
				List<int> result = null;

				if(ViewState["courseid"] != null)
				{
					result = ViewState["courseid"] as List<int>;
				}
				else
				{
					result = CommonHelper.QueryStringInts("courseid");
				}

				return result;
			}
			set
			{
				ViewState["courseid"] = value;
			}
		}

		private int? PayCredits
		{
			get;
			set;
		}

		public User CurrentUser
		{
			get
			{
				return SiteContext.Current.CurrentUser;
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			if(!IsPostBack)
			{
				BindCourse();
			}
		}

		protected void Page_PreRender(object sender, EventArgs e)
		{
			if(CurrentUser != null)
			{
				//if(CurrentUser.Credits.HasValue)
				{
					lblRemainingCredits.Text = "您当前剩余积分:" + CurrentUser.Credits.GetValueOrDefault(0).ToString();
				}
				//if(PayCredits.HasValue)
				{
					lblPayCredits.Text = "所需积分:" + PayCredits.GetValueOrDefault(0).ToString();
				}

				btnSubmit.Visible = (CurrentUser.Credits >= PayCredits.GetValueOrDefault(0));

				if(CurrentUser.Credits.GetValueOrDefault(0) < PayCredits.GetValueOrDefault(0))
				{
					lblMsg.Text = "您的积分不够";
				}
				else
				{
					lblMsg.Text = string.Empty;
				}
			}
		}

		protected void gvCourse_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			if(e.Row.DataItem != null)
			{
				var lbtnDelete = e.Row.FindControl("lbtnDelete") as LinkButton;
				var dataItem = e.Row.DataItem as Course;
				var ltrCredits = e.Row.FindControl("ltrCredits") as Literal;

				lbtnDelete.CommandArgument = dataItem.CourseID.ToString();

				var courseChapters = GetCourseChapter(dataItem.CourseID);

				if(courseChapters != null && courseChapters.Count > 0)
				{
					var credits = courseChapters.Sum(cc => cc.CourseChapterReqiredCredits);

					PayCredits = PayCredits.GetValueOrDefault(0) + credits;
					ltrCredits.Text = credits.ToString();
				}
			}
		}

		private List<CourseChapter> GetCourseChapter(int courseID)
		{
			List<CourseChapter> result = null;

			if(courseID > 0)
			{
				if(_CourseProvider == null)
				{
					_CourseProvider = new CourseProvider();
				}

				result = _CourseProvider.GetCourseChapterByCourse(courseID);
			}

			return result;
		}

		protected void btnSubmit_Click(object sender, EventArgs e)
		{
			var user = SiteContext.Current.CurrentUser;

			if(user != null)
			{
				var lstUserAndCourse = new List<RLT_USERANDCOURCE>();
				var credits = 0;

				foreach(GridViewRow row in gvCourse.Rows)
				{
					var lbtnDelete = row.FindControl("lbtnDelete") as LinkButton;
					var ltrCredits = row.FindControl("ltrCredits") as Literal;
					var theCourseID = int.Parse(lbtnDelete.CommandArgument);
					var theRLTUserAndCourse = new RLT_USERANDCOURCE() { CourseID = theCourseID, UserID = user.UserID };
					
					lstUserAndCourse.Add(theRLTUserAndCourse);
					var c = int.Parse(ltrCredits.Text.Trim());

					credits += c;
				}

				if(_CourseProvider == null)
				{
					_CourseProvider = new CourseProvider();
				}

				_CourseProvider.AddRLTUserAndCourse(lstUserAndCourse);

				var userProvider = new UserProvider();
				CurrentUser.Credits = CurrentUser.Credits - credits;
				userProvider.UpdateUser(CurrentUser, false);

				Response.Redirect("~/CourseCenter.aspx");
			}
		}

		protected void gvCourse_RowCommand(object sender, GridViewCommandEventArgs e)
		{
			if(e.CommandName.Equals("remove"))
			{
				var courseID = int.Parse(e.CommandArgument.ToString());
				var ids = CourseIDs;
				ids.Remove(courseID);

				CourseIDs = ids;

				BindCourse();
			}
		}

		private void BindCourse()
		{
			if(CourseIDs.Count > 0)
			{
				if(_CourseProvider == null)
				{
					_CourseProvider = new CourseProvider();
				}

				gvCourse.DataSource = _CourseProvider.GetCourseByCourseIDs(CourseIDs);
				gvCourse.DataBind();
			}
			else
			{
				gvCourse.DataSource = null;
				gvCourse.DataBind();
			}
		}
	}
}